// pages/order/check.js
const app = getApp();

let req = require('../../utils/request.js');
let api = require('../../utils/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModalStatus: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this,
        id = options.id
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    req.request(
      api.getUserOrderById,
      {method: 'POST', data: {id: id}},
      function(res) {
        res = res.data
        var create_time = res.order_info.create_time,
            date = new Date(create_time),
            timestamp = date.getTime(),
            last_time = new Date(timestamp + 900000),
            h = (last_time.getHours() < 10 ? '0' + last_time.getHours() : last_time.getHours()) + ' : ',
            m = (last_time.getMinutes() < 10 ? '0' + last_time.getMinutes() : last_time.getMinutes()) + ' : ',
            s = last_time.getSeconds() < 10 ? '0' + last_time.getSeconds() : last_time.getSeconds()
        _this.setData({order_info: res, last_time: h+m+s})
        wx.hideLoading();
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  Pay(){
    wx.showLoading({
      title: '加载中',
    })
    var id = this.data.order_info.order_info.order_id
    
    req.request(
      api.userPayments,
      {method: 'POST', data: {id: id}},
      function(res) {
        wx.hideLoading();
        res = res.data

        wx.showToast({
          title: res.msg,
          icon: 'none'
        })

        if(res.status == 1) {
          setTimeout(function() {
            wx.redirectTo({
              url: 'detail?id='+id,
            })
          }, 2000)
        }
      }
    )
  },
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  }
})